package com.lzs.esubscribe.controller;

import com.lzs.esubscribe.api.CommonResult;
import com.lzs.esubscribe.dto.LoginDTO;
import com.lzs.esubscribe.dto.ModifyPasswordDTO;
import com.lzs.esubscribe.dto.RegisterDTO;
import com.lzs.esubscribe.dto.UrlListDTO;
import com.lzs.esubscribe.entity.ESUser;
import com.lzs.esubscribe.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lzs
 * @Date: 2022-03-17
 * @Description：
 */
@ResponseBody
@RestController
@RequestMapping("/user")
@Api(value = "用户")
public class UserController {

    @Autowired
    IUserService iUserService;

    /**
     * @param
     * @return
     * @description 用户注册
     * @date 2022-03-17
     */
    @ApiOperation(value = "用户注册", notes = "用户注册", httpMethod = "POST", response = CommonResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/register")
    public CommonResult register(@RequestBody RegisterDTO registerDTO) {
        int res = iUserService.register(registerDTO);
        if (res != 0) {
            return CommonResult.success("注册成功");
        } else {
            return CommonResult.failed("注册错误，可能是用户名已被注册");
        }
    }

    /**
     * @param
     * @return
     * @description 用户登录
     * @date 2022-03-17
     */
    @ApiOperation(value = "用户登录", notes = "用户登录", httpMethod = "POST", response = ESUser.class)
    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public CommonResult login(@RequestBody LoginDTO loginDTO) {
        ESUser res = iUserService.login(loginDTO);
        if (res != null) {
            return CommonResult.success(res,"登录成功");
        } else {
            return CommonResult.failed("用户名或密码不正确");
        }
    }

    /**
     * @param
     * @return
     * @description 修改用户密码
     * @date 2022-03-17
     */
    @ApiOperation(value = "修改用户密码", notes = "修改用户密码", httpMethod = "POST", response = CommonResult.class)
    @RequestMapping(method = RequestMethod.POST, value = "/modifyPassword")
    public CommonResult modifyPassword(@RequestBody ModifyPasswordDTO modifyPasswordDTO) {
        int res = iUserService.modifyPassword(modifyPasswordDTO);
        if (res != 0) {
            return CommonResult.success("修改密码成功");
        } else {
            return CommonResult.failed("原密码不正确");
        }
    }

    /**
     * @param
     * @return
     * @description 查询用户收藏资源
     * @date 2022-03-17
     */
    @ApiOperation(value = "查询用户收藏资源", notes = "查询用户收藏资源", httpMethod = "GET",response = UrlListDTO.class)
    @RequestMapping(method = RequestMethod.GET, value = "/getUserSubscribeTree")
    public CommonResult getUserSubscribeTree(@ApiParam(name = "uuid",value = "用户uuid",required = true) @RequestParam("uuid") String uuid) {
        List<UrlListDTO> userSubscribeTree = iUserService.getUserSubscribeTree(uuid);
        if (userSubscribeTree != null) {
            return CommonResult.success(userSubscribeTree,"查询成功");
        } else {
            return CommonResult.failed("查询失败");
        }
    }


}
